new Vue({
    el: '#container',
    delimiters: ['${', '}'],
    data() {
        return {
            autoParamsList: [{
                auto_key: '',
                auto_value: '',
            }],
            auto_msg: '',
            baseUrl: '',
            account: '',
            password: '',
            checked: true,
            groupName: '',
            isShow: false,
            api_path: '',
            apiType: 'GET',
            api_id: '',
            api_params: '',
            api_name: '',
            api_method: '',
            api_desc: '',
            resultJson: {}
        }
    },
    mounted() {

    },
    methods: {
        // 添加参数
        addAutoParams() {
            this.autoParamsList.push({
                auto_key: '',
                auto_value: '',
            })
        },
        //添加弹框
        autoLoginDialog() {
            this.autoParamsList[{
                auto_key: '',
                auto_value: '',
            }]
            $('#myDialog').modal()
        },
        //删除参数
        deleteApiParams(index, item) {
            console.log(index)
            this.autoParamsList.splice(index, 1)
        },
        // 授权登录
        autoLogin() {
            let mapObj = {};
            for (let item of this.autoParamsList) {
                mapObj[item.auto_key] = item.auto_value;
            }

            let param = JSON.stringify(this.checked ? ({
                encry: JWTSecret.jwt(mapObj)
            }) : mapObj);
            mapObj.path = '{{result[0].system_url}}' + this.baseUrl;
            mapObj.parmas = param;
            $.ajax({
                url: '/index/sendApiResult',
                type: 'POST',
                data: mapObj,
            }).then((json) => {
                if (json.success) {
                    this.auto_msg = '授权成功';
                } else {
                    this.auto_msg = `授权失败，${json.msg}`;
                }
            }).catch(function (error) {
                console.log(error);
            });
        },

        //清除授权
        clearAuto() {
            $.ajax({
                url: '/index/clear',
                type: 'POST',

            }).then((json) => {
                if (json.success) {
                    console.log('清除成功')
                } else {
                    console.log('清除失败')
                }
            }).catch(function (error) {
                console.log(error);
            });
        },


        clickMethods(id, groupName) {
            this.isShow = true;
            this.resultJson = {};
            $.get(`/index/show?api_id=${id}`).then((json) => {
                let res = json.result;
                if (json.success) {
                    this.api_path = res.api_path;
                    this.api_id = res.api_id;
                    this.api_params = JSON.parse(res.api_params);
                    this.api_name = res.api_name;
                    this.api_method = res.api_method;
                    this.api_desc = res.api_desc;
                    this.groupName = groupName
                }
            }).catch(function (error) {
                console.log(error);
            });
        },
        //请求第三方接口
        sendAjaxApi() {
            let paramsObj = this.formartParams();
            let param = JSON.stringify(this.checked ? ({
                encry: JWTSecret.jwt(paramsObj.parmas)
            }) : paramsObj.parmas);
            paramsObj.path = this.api_path + this.api_method;
            paramsObj.parmas = param;
            //  console.log(paramsObj);
            $.ajax({
                url: '/index/send',
                type: this.apiType,
                //  url: this.api_path + this.api_method,
                data: paramsObj,
            }).then((json) => {
                this.resultJson = json;
            }).catch(function (error) {
                console.log(error);
            });
        },
        formartParams() {
            let mapObj = {};
            for (let item of this.api_params) {
                mapObj[item.param_key] = item.param_value;
            }
            return {
                parmas: mapObj
            }
        },


    }
})